package com.perkinelmer.housecall.client.activities.capture.impl;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.perkinelmer.housecall.client.activities.capture.Capture;
import com.perkinelmer.housecall.client.activities.capture.CaptureParameter;
import com.perkinelmer.housecall.client.model.Observation;

public class VitalsCapture implements Capture {

	public static final CaptureId ID = CaptureId.make("VitalsCapture");
	
	private final Grid form;
	private final Button submitButton;
	private final Button cancelButton;
	
	private final TextBox temperatureField;
	private final TextBox bloodPressureField;
	private final TextBox pulseField;
	
	private HandlerRegistration submitHandlerReg = null;
	private HandlerRegistration cancelHandlerReg = null;
	
	public VitalsCapture() {
		submitButton = new Button("Submit");
		cancelButton = new Button("Cancel");
		
		form = new Grid(0, 2);
		form.setCellSpacing(5);
	
		temperatureField = addField(form, "Temperature");
		bloodPressureField = addField(form, "Blood Pressure");
		pulseField = addField(form, "Pulse");
	}
	
	@Override
	public CaptureId getId() {return ID;}

	@Override
	public String getName() {
		return "Vitals";
	}
	
	@Override
	public void start(Panel panel, final CaptureParameter captureParameter) {
		clearFields();
		
		updateSubmitClickHandler(captureParameter._callback);
		updateCancelClickHandler(captureParameter._callback);
		
		VerticalPanel capturePanel = new VerticalPanel();
		capturePanel.setSpacing(5);
		capturePanel.add(new Label("Capture Vitals"));
		capturePanel.add(form);
		capturePanel.add(createButtonPanel());
		
		panel.add(capturePanel);
	}
	
	private Panel createButtonPanel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(5);
		panel.add(submitButton);
		panel.add(cancelButton);
		
		return panel;
	}
	
	private void updateSubmitClickHandler(final CaptureCompleteCallback callback) {
		if(submitHandlerReg != null) {submitHandlerReg.removeHandler();}
		submitHandlerReg = submitButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				VitalsObservation observation = createObservation();
				callback.captureComplete(observation);
			}
		});
	}
	
	private void updateCancelClickHandler(final CaptureCompleteCallback callback) {
		if(cancelHandlerReg != null) {cancelHandlerReg.removeHandler();}
		cancelHandlerReg = cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				callback.captureCanceled();
			}
		});
	}
	
	private VitalsObservation createObservation() {
		String temperature = temperatureField.getText();
		String bloodPressure = bloodPressureField.getText();
		String pulse = pulseField.getText();
		
		return new VitalsObservation(temperature, bloodPressure, pulse);
	}
	
	private void clearFields() {
		temperatureField.setText("");
		bloodPressureField.setText("");
		pulseField.setText("");
	}
	
	private TextBox addField(Grid form, String fieldName) {
		Label label = new Label(fieldName);
		TextBox input = new TextBox();
		input.setName(fieldName);
		input.setWidth("75px");
		
		int rowIndex = form.getRowCount();
		form.resizeRows(rowIndex + 1);
		
		form.setWidget(rowIndex, 0, label);
		form.setWidget(rowIndex, 1, input);
		
		return input;
	}
	
	public static class VitalsObservation extends Observation {

		public static final ObservationId ID = ObservationId.make("Vitals");
		
		public final String temperature;
		public final String bloodPressure;
		public final String pulse;
		
		public VitalsObservation(String temperature, String bloodPressure, String pulse) {
			this.temperature = temperature;
			this.bloodPressure = bloodPressure;
			this.pulse = pulse;
		}
		
		@Override
		public ObservationId getId() {return ID;}
	}
}
